import request from './index'

/**
 * 入库统计API
 */
export const storageStatisticsApi = {
  /**
   * 分页查询入库统计列表
   */
  getPage(params) {
    return request({
      url: '/api/datacollection/storage-statistics',
      method: 'get',
      params
    })
  },

  /**
   * 获取统计概览
   */
  getOverview(params) {
    return request({
      url: '/api/datacollection/storage-statistics/overview',
      method: 'get',
      params
    })
  },

  /**
   * 获取所有数据类型
   */
  getDataTypes() {
    return request({
      url: '/api/datacollection/storage-statistics/data-types',
      method: 'get'
    })
  },

  /**
   * 导出入库统计数据
   */
  exportData(params) {
    return request({
      url: '/api/datacollection/storage-statistics/export',
      method: 'get',
      params,
      responseType: 'blob'
    }).then(response => {
      // 创建下载链接
      const blob = new Blob([response.data], { 
        type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' 
      })
      const url = window.URL.createObjectURL(blob)
      const link = document.createElement('a')
      link.href = url
      
      // 从响应头获取文件名，如果没有则使用默认名称
      const contentDisposition = response.headers['content-disposition']
      let filename = 'storage_statistics.xlsx'
      if (contentDisposition) {
        const filenameMatch = contentDisposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/)
        if (filenameMatch && filenameMatch[1]) {
          filename = filenameMatch[1].replace(/['"]/g, '')
        }
      }
      
      link.download = filename
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
      window.URL.revokeObjectURL(url)
    })
  },

  /**
   * 更新增长率
   */
  updateGrowthRates(params) {
    return request({
      url: '/api/datacollection/storage-statistics/update-growth-rates',
      method: 'post',
      params
    })
  },

  /**
   * 生成测试数据
   */
  generateTestData(params) {
    return request({
      url: '/api/datacollection/storage-statistics/generate-test-data',
      method: 'post',
      params
    })
  }
}

/**
 * 数据采集通用API
 */
export const dataCollectionApi = {
  /**
   * 获取数据采集概览
   */
  getOverview() {
    return request({
      url: '/api/datacollection/overview',
      method: 'get'
    })
  },

  /**
   * 获取数据采集状态
   */
  getStatus() {
    return request({
      url: '/api/datacollection/status',
      method: 'get'
    })
  }
}
